﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FeaturesBlog.Models.Interfaces;

namespace FeaturesBlog.Models.Repositories
{
    public class PostRepository : IPostRepository
    {
        #region Variables
        private MyFeaturesBlogClassesDataContext _context;
        #endregion

        #region Methods
        public PostRepository(MyFeaturesBlogClassesDataContext c)
        {
            _context = c;
        }
        public Post GetPost(decimal postId)
        {
            return _context.Posts.SingleOrDefault(p => p.PostId == postId);
        }
        public IEnumerable<Post> GetPosts(int currentPage, int pageSize)
        {
            return _context.Posts.Where(p => p.Active == true).OrderByDescending(p => p.PostDate).Skip(currentPage*pageSize).Take(pageSize);
        }
        public int GetPostsCount()
        {
            return _context.Posts.Where(p => p.Active == true).Count();
        }
        public void AddPost(Post p)
        {
            _context.Posts.InsertOnSubmit(p);
            _context.SubmitChanges();
        }
        public void EditPost(Post p)
        {
            Post DbPost = GetPost(p.PostId);
            DbPost.PostBody = p.PostBody;
            DbPost.PostSubject = p.PostSubject;
            _context.SubmitChanges();
        }
        public void DeletePost(decimal postId)
        {
            Post DbPost = GetPost(postId);
            DbPost.Active = false;
            _context.SubmitChanges();
        }
        #endregion
    }
}
